using System;
using System.Runtime.InteropServices;
internal class Win32API
{

	#region " Constants "
	#region " GDI "
	public const int SRCCOPY = 13369376;
	#endregion

	#region " Windows "
	//Menu item constants
	public const long SC_SIZE = 61440l;
	public const long SC_SEPARATOR = 61455l;
	public const long SC_MOVE = 61456l;
	public const long SC_MINIMIZE = 61472l;
	public const long SC_MAXIMIZE = 61488l;
	public const long SC_CLOSE = 61536l;
	public const long SC_RESTORE = 61728l;

	const int WM_VSCROLL = 277;

	#endregion

	//Scrollbar:
	const int SB_VERT = 1;
	const int EM_SETSCROLLPOS = 1024 + 222;
	const int SB_THUMBPOSITION = 4;
	const int SB_BOTTOM = 7;

	#endregion

	#region " Types "
	public struct RECT
	{
		public int Left;
		public int Top;
		public int Right;
		public int Bottom;
	}

	public struct POINTAPI
	{
		public int x;
		public int y;
	}
	[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int SelectObject(int hDC, int hObject);
	[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int DeleteObject(int hObject);
	[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int GetDC(int hWnd);
	[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int GetWindowDC(int hWnd);
	[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int CreateCompatibleDC(int hDC);
	[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int DeleteDC(int hDC);
	[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int ReleaseDC(int hWnd, int hDC);
	[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int CreatePen(int nPenStyle, int nWidth, int crColor);
	[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int CreateSolidBrush(int crColor);
	[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int BitBlt(int hDestDC, int X, int Y, int nWidth, int nHeight, int hSrcDC, int xSrc, int ySrc, int dwRop);
	[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int CreateCompatibleBitmap(int hDC, int nWidth, int nHeight);
	[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int FillRect(int hDC, ref RECT lpRect, int hBrush);
	[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int LineTo(int hDC, int X, int Y);
	[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int MoveToEx(int hDC, int X, int Y, ref POINTAPI lpPoint);
	[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int Ellipse(int hDC, int X1, int Y1, int X2, int Y2);
	[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int Rectangle(int hDC, int X1, int Y1, int X2, int Y2);
	[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern Int32 LockWindowUpdate(Int32 hwndLock);
	[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern bool GetScrollRange(IntPtr hWnd, int nBar, ref int lpMinPos, ref int lpMaxPos);
	[DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, ref POINTAPI lParam);
	[DllImport("WinMM.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int timeGetTime();
	[DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int QueryPerformanceCounter(ref Int64 PC);
	[DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int QueryPerformanceFrequency(ref Int64 PC);
	#endregion

	#region " Macros "

	#region " GDI "
	public static int RGB(int R, int G, int B)
	{
		return R | (G * 256) | (B * 65536);
	}

	public static RECT ToRECT(System.Drawing.Rectangle r)
	{
		RECT functionReturnValue;
		functionReturnValue.Left = r.Left;
		functionReturnValue.Top = r.Top;
		functionReturnValue.Right = r.Right;
		functionReturnValue.Bottom = r.Bottom;
		return functionReturnValue;
	}

	public static void FillRectangle(int DC, System.Drawing.Color Col, System.Drawing.Rectangle r)
	{
		int hBrush = CreateSolidBrush(System.Drawing.ColorTranslator.ToWin32(Col));
		RECT rect = ToRECT(r);
		FillRect(DC, ref rect, hBrush);
		DeleteObject(hBrush);
	}
	#endregion

	#region " Controls "

	public static void ScrollTextBoxToEnd(IntPtr Handle, int TextBoxHeight)
	{
		if (Handle.Equals(IntPtr.Zero)) return; 

		POINTAPI p = new POINTAPI();
		Win32API.SendMessage(Handle, WM_VSCROLL, SB_BOTTOM, ref p);
	}

	#endregion

	#region " VB "

	public static void Swap(ref double x1, ref double x2)
	{
		double t = x1;
		x1 = x2;
		x2 = t;
	}

	public static void Order(ref double x1, ref double x2)
	{
		if (x1 > x2)
		{
			double t = x1;
			x1 = x2;
			x2 = t;
		}
	}

	#endregion

	#endregion

}
